/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.search.concurrent;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;

import com.kingcore.director.core.util.StopWatch;
import com.kingcore.director.search.ui.LocalFileIndexBuilder;
import com.kingcore.director.search.ui.LocalFileIndexManager;

/**
 * <p>java class file describe</p>
 * @author Zeven on 2009-3-18
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class FileConsumerDataListProducer implements Runnable {

	//log object
	private final static Logger log = Logger.getLogger(FileConsumerDataListProducer.class);
	private static BlockingQueue<File> fielQueue = null;
	private static BlockingQueue<List<Map<String,String>>> dataListqueue = null;

	public FileConsumerDataListProducer(BlockingQueue<File> p_fielQueue,
			BlockingQueue<List<Map<String, String>>> p_dataListqueue) {
		fielQueue = p_fielQueue;
		dataListqueue = p_dataListqueue;
	}

	/**
	 * <p>java method describe</p>
	 * @author Zeven on 2011-3-18
	 * @param args
	 */
	public static void main(String[] args) { 

	}

	public void run() {
		File f = null;
		while (true) {
			try {
				f = fielQueue.take();
				if( f.getParent()==null || f.getParent().equals( FileProducer.File_End_Flag )){
					addFile2List(null, "insert");   //对应不存在的文件，f或许不为Null，但是f.getParent()肯定为Null.
					return ; //stop
					
				}else if (f.isDirectory()) {
					Map<String,String> map = new HashMap<String,String>();
					map.put("name", f.getName());
					map.put("path", f.getParent()); //f.getAbsolutePath()
					map.put("size", "");
					map.put("lastModifiedTime", f.lastModified()+"");
					addFile2List(map, "insert"); 

				} 
				else {
					//System.out.println(f.getAbsolutePath()); 
					Map<String,String> map = new HashMap<String,String>();
					map.put("name", f.getName());
					map.put("path", f.getParent()); //f.getAbsolutePath()
					map.put("size", f.length()+"");
					map.put("lastModifiedTime", f.lastModified()+"");
					addFile2List(map, "insert"); 
				}
				
			} catch (InterruptedException e) { 
				e.printStackTrace();
			}
			
		}
		
	}

	private static StopWatch sw = new StopWatch();
	public static int Batch_Size_Write_To_Document = 5000;
	private static List<Map<String,String>>  fileList = new ArrayList<Map<String,String>>();
	
	private static void addFile2List(Map<String,String> map, String action) {
		if ("delete".equals(action)) {  //delete index file
			LocalFileIndexManager im = null;
			try {
				im = new LocalFileIndexManager(LocalFileIndexBuilder.LocalFileSystem_INDEX);
				im.deleteIndexByIndex( map.get("path") + File.separator + map.get("name") );
				im.close();
			} catch (IOException e) { 
				e.printStackTrace();
			}
			return ;
		}
			
		if (map != null) {
			fileList.add(map);
		}
		
		if (map == null || fileList.size() == Batch_Size_Write_To_Document) {
			try {
				dataListqueue.put(fileList);
				log.info("put to dataListqueue ...queue size is:"+dataListqueue.size()); 
				fileList = new ArrayList<Map<String,String>>();
					
			} catch (InterruptedException e) { 
				e.printStackTrace();
			}
			log.info( sw.watchPoint() );
		}
	 }

}
